package models

import "github.com/beego/beego/v2/client/orm"

type ErpCuringData struct {
	Id              int64
	DepartmentId    int64
	Curing          *ErpCuring    `orm:"rel(fk);null;on_delete(do_nothing)"`
	Products        *ErpProducts  `orm:"rel(fk);null;on_delete(do_nothing)"`
	Inventory       *ErpInventory `orm:"rel(fk);null;on_delete(do_nothing)"`
	LotNumber       string        `json:"lot_number"`       //批号
	ManufactureDate int64         `json:"manufacture_date"` //生产日期
	ExpiryDate      int64         `json:"expiry_date"`      //有效日期
	Exterior        int           `json:"exterior"`         //外观情况1合格2不合格
	Packing         int           `json:"packing"`          //包装情况1合格2不合格
	Opinion         int           `json:"opinion"`          //处理意见1合格2不合格
	Status          int           `json:"status"`           //养护状态0待养护1养护中2已完成
}

func (a *ErpCuringData) TableName() string {
	return ErpCuringDataTableName()
}
func ErpCuringDataTableName() string {
	return TableName("erp_curing_data")
}

//盘点主表查询项
type ErpCuringDataQueryParam struct {
	BaseQueryParam
	Curing int64 `json:"curing_id"`
	Depid  int64 `json:"depid"`
	Status []int `json:"status"` //养护状态0待养护1养护中2已完成
}

//查询条件
func ErpCuringDataQuerySeter(params *ErpCuringDataQueryParam) orm.QuerySeter {

	query := orm.NewOrm().QueryTable(ErpCuringDataTableName()).RelatedSel("Curing", "Products", "Inventory", "Inventory__Snd")

	cond := orm.NewCondition()
	if params.Depid > 0 {
		cond = cond.And("Curing__department_id", params.Depid)
	}
	if params.Curing > 0 {
		cond = cond.And("curing_id", params.Curing)
	}
	if len(params.Status) > 0 {
		cond = cond.And("Status__in", params.Status)
	}
	query = query.SetCond(cond)

	return query
}

func ErpCuringDataPageList(params *ErpCuringDataQueryParam) ([]*ErpCuringData, int64) {

	ls := make([]*ErpCuringData, 0)

	query := ErpCuringDataQuerySeter(params)

	total, _ := query.Count()

	query.OrderBy("-Id").Limit(params.Limit, params.Offset).All(&ls)

	return ls, total
}
